﻿#ifndef __INI_CONFIGURATION_FILE_ACCESS_CLASS_H__
#define __INI_CONFIGURATION_FILE_ACCESS_CLASS_H__


#include <vector>
#include <string>

#include <QString>
#include <QVariant>

#include <QLibrary>
#include <QLibraryInfo>


typedef unsigned long (__stdcall *PFN_GetPrivateProfileSectionNamesA)(char* lpszReturnBuffer, unsigned long nSize, const char* lpFileName);
typedef unsigned long (__stdcall *PFN_GetPrivateProfileSectionA)(const char* lpAppName, char* lpReturnedString, unsigned long nSize, const char* lpFileName);
typedef unsigned long (__stdcall *PFN_GetPrivateProfileStringA)(const char* lpAppName, const char* lpKeyName, const char* lpDefault, char* lpReturnedString, unsigned long nSize, const char* lpFileName);
typedef int (__stdcall *PFN_WritePrivateProfileStringA)(const char* lpAppName, const char* lpKeyName, const char* lpString, const char* lpFileName);


/// <summary>
/// Windows 下 .ini 文件的访问类
/// </summary>
///

class QIniFile
{
    PFN_GetPrivateProfileSectionNamesA fnGetPrivateProfileSectionNamesA;
    PFN_GetPrivateProfileSectionA fnGetPrivateProfileSectionA;
    PFN_GetPrivateProfileStringA fnGetPrivateProfileStringA;
    PFN_WritePrivateProfileStringA fnWritePrivateProfileStringA;

public:
    QIniFile();
    QIniFile(const char* sPath);
    virtual ~QIniFile();

	void  LoadFile(const char* sPath); //文件路径

	void  GetSectionNames(std::vector<std::string>& ar);/*得到所有段*/

	void  GetKeyNames(const char* lpSection/*段名*/, std::vector<std::string>& ar);//得到某段下所有key名称


    unsigned long GetValue(const char* lpSection/*段名*/, const char* sKey/*Key值*/, std::string& key);//得到可以值
    unsigned long GetValue(const QString& section/*段名*/, const QString& key/*Key值*/, QVariant& val);

	void  GetKey_Value(const char* lpSection/*段名*/, std::vector<std::string>& ar);/*得到该段下的所有key-value*/


    int  SetValue(const char* lpSection, const char* lpKey, const char* lpValue);//写入键值
    int  SetValue(const QString& section, const QString& key, const QString& val);

    int  DeleteSection(const char* lpSection);//删除段
    int  DeleteKey(const char* lpSection, const char* lpKey);//删除key

protected:
    unsigned long GetSectionNames(char* lpBuffer, unsigned long dwBufSize);//返回字符总数
    unsigned long GetKey_Value(const char* lpSection, char* lpBuffer, unsigned long dwBufSize);//返回字符总数

    int  ParseString(char* buff, std::vector<std::string>& ar);//解析出段、键值


protected:
	std::string m_sPath;
};


#endif //__INI_CONFIGURATION_FILE_ACCESS_CLASS_H__
